# from majiang.Commondef import *  # NOQA
from . import Commondef


class MahJongTile(object):
    """docstring for MahJongTile"""

    def __init__(self, tileType, rank=0):
        self.type = tileType
        self.rank = rank

    def __eq__(self, other):
        if self.type == other.type and self.rank == other.rank:
            return True
        else:
            return False

    def __ne__(self, other):
        return not self == other

    def __gt__(self, other):
        if self.type > other.type:
            return True
        elif self.type < other.type:
            return False
        else:
            if self.rank > other.rank:
                return True
            else:
                return False

    def __ge__(self, other):
        if self == other or self > other:
            return True
        else:
            return False

    def __lt__(self, other):
        if self > other or self == other:
            return False
        else:
            return True

    def __le__(self, other):
        if self > other:
            return False
        else:
            return True

    def IsValueTo(self, other):

        if (self.type == other.type and
                abs(self.rank - other.rank) < 3):
            return True
        else:
            return False

    def __str__(self):
        return str(self.type) + str(self.rank)


class MahJong(object):
    """docstring for MahJong"""
    index = 0

    def __init__(self, rule):
        self.rule = rule
        self.tiles = []
        self.CreateTiles()

    # region publicFunction
    def CreateTiles(self):
        mj = MahJongCreater()
        self.tiles = mj.CreateMahJongByRule(self.rule["TilesType"])

    def Shuffle(self):
        Commondef.random.shuffle(self.tiles)
        self.index = 0

    def Deal(self, count=1):
        out = self.tiles[self.index:self.index + count]
        self.index += count
        return out

    def GetTilesLeft(self):
        return self.tiles[self.index:]

    def getTilesOut(self):
        return self.tiles[:self.index]
    # endregion


# region privateFunction

    def _SafeFun(self):
        pass

    # endregion


class MahJongCreater(object):
    """docstring for MahJongCreater"""

    def __init__(self):
        pass

    def CreateKindGroup(self, dwGroudKind):
        ret = []
        cfg = Commondef.majiangConfig[dwGroudKind]
        for type in xrange(cfg["members"][0], cfg["members"][-1] + 1):
            for rank in xrange(1, cfg["maxRank"] + 1):
                for index in xrange(1, cfg["maxCount"] + 1):
                    tile = MahJongTile(type, rank)
                    ret.append(tile)
        return ret

    def CreateMahJongByRule(self, ruleType):
        ret = []
        if ruleType == Commondef.TilesType.Base:
            ret = self.CreateKindGroup(Commondef.KindGroup.Suits)
        return ret
